package com.example.demo.controllers;

import com.example.demo.common.utils.ToolsUtil;
import com.example.demo.entity.HD;
import com.example.demo.entity.KH;
import com.example.demo.entity.PS;
import com.example.demo.services.PSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: Ding XiJie
 */
@RequestMapping("/ps")
@Controller
public class PSController {
    @Autowired
    private PSService psService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加配送
     */
    @RequestMapping("/save")
    public String save(@RequestBody PS ps, Model model){
        try {
            ps = psService.save(ps);
            model.addAttribute("msg","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","添加失败");
        }
        return "ps.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改配送
     */
    @RequestMapping("/update")
    public String update(@RequestBody PS ps, Model model){
        try {
            ps = psService.update(ps);
            model.addAttribute("msg","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","修改失败");
        }
        return "ps.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除配送
     */
    @RequestMapping("/delete")
    public String delete(int psid, Model model){
        try {
            psService.delete(psid);
            model.addAttribute("msg","删除成功");
        }catch (Exception e){
            model.addAttribute("msg","删除失败");
            e.printStackTrace();
        }
        return "ps.html";
    }

   /* *//**
     * @Author: Ding XiJie
     * @Description: 根据用户kid查询用户货单信息
     *//*
    @RequestMapping("/getKhByKid")
    @ResponseBody
    public Map<String, Object> getKhByKid(Integer kid){
        try {
            KH kh = khService.getKhByKid(kid);
            return ToolsUtil.returnTable(0,1,"查询成功",kh);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,0,"查询失败",null);
        }
    }*/

    /**
     * @Author: Ding XiJie
     * @Description: 跳转页面
     */
    @RequestMapping("/toPs")
    public String toPs(Model model){
        return "user_ps.html";
    }
}
